package com.flipdf2.serialize.ppml.element;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;

public class PPMLMark {
	public static final String NAME = "MARK";
	
	private List<PPMLTicketSet> ticketSetList;
	private List<PPMLTicketRef> ticketRefList;
	
	private PPMLView view;
	private List<PPMLObject> objectList;
	
	private PPMLOccurrenceRef occurrenceRef;
	private PPMLSegmentRef segmentRef;

	private double[] position;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (ticketSetList != null && ticketSetList.size() > 0) {
			for (int i = 0, n = ticketSetList.size(); i < n; i++) {
				root.addContent(ticketSetList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (ticketRefList != null &&ticketRefList.size() > 0) {
			for (int i = 0, n = ticketRefList.size(); i < n; i++) {
				root.addContent(ticketRefList.get(i).toXML(defaultNamespace));
			}
		}

		if (view != null)
			root.addContent(view.toXML(defaultNamespace));
		
		if (objectList != null &&objectList.size() > 0) {
			for (int i = 0, n = objectList.size(); i < n; i++) {
				root.addContent(objectList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (occurrenceRef != null)
			root.addContent(occurrenceRef.toXML(defaultNamespace));
		
		if (segmentRef != null)
			root.addContent(segmentRef.toXML(defaultNamespace));
		
		if (position != null) 
			root.setAttribute("Position", position[0] + " " + position[1]);
		
		return root;
	}
	
	public void check() throws PPMLException {
		if (position == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Position");
		}
		if (occurrenceRef == null && segmentRef == null 
				&& objectList == null && view == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ELEMENT_LOST
					, "((VIEW?, OBJECT+) | OCCURRENCE_REF | SEGMENT_REF)");
		}
	}

	public List<PPMLObject> getObjectList() {
		return objectList;
	}

	public void setObjectList(List<PPMLObject> objectList) {
		this.objectList = objectList;
	}
	
	public void addObject(PPMLObject object) {
		if (this.objectList == null)
			this.objectList = new ArrayList<PPMLObject>();
		this.objectList.add(object);
	}

	public PPMLOccurrenceRef getOccurrenceRef() {
		return occurrenceRef;
	}

	public void setOccurrenceRef(PPMLOccurrenceRef occurrenceRef) {
		this.occurrenceRef = occurrenceRef;
	}

	public double[] getPosition() {
		return position;
	}

	public void setPosition(double[] position) {
		this.position = position;
	}

	public PPMLSegmentRef getSegmentRef() {
		return segmentRef;
	}

	public void setSegmentRef(PPMLSegmentRef segmentRef) {
		this.segmentRef = segmentRef;
	}

	public List<PPMLTicketRef> getTicketRefList() {
		return ticketRefList;
	}

	public void setTicketRefList(List<PPMLTicketRef> ticketRefList) {
		this.ticketRefList = ticketRefList;
	}
	
	public void addTicketRef(PPMLTicketRef ticketRef) {
		if (this.ticketRefList == null)
			this.ticketRefList = new ArrayList<PPMLTicketRef>();
		this.ticketRefList.add(ticketRef);
	}

	public List<PPMLTicketSet> getTicketSetList() {
		return ticketSetList;
	}

	public void setTicketSetList(List<PPMLTicketSet> ticketSetList) {
		this.ticketSetList = ticketSetList;
	}
	
	public void addTicketSet(PPMLTicketSet ticketSet) {
		if (this.ticketSetList == null)
			this.ticketSetList = new ArrayList<PPMLTicketSet>();
		this.ticketSetList.add(ticketSet);
	}
	
	public PPMLView getView() {
		return view;
	}

	public void setView(PPMLView view) {
		this.view = view;
	}
}
